﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class Mp3Video
    {
        private string _VideoName;
        private string _SingerName;  
        private string _DirectLink;
        private string _ImgLink;
        private string _LocalLink;

        public string VideoName
        {
            get { return _VideoName; }
            private set { _VideoName = value; }
        }
        public string SingerName
        {
            get { return _SingerName; }
            private set { _SingerName = value; }
        }
        public string ImgLink
        {
            get { return _ImgLink; }
            private set { _ImgLink = value; }
        }
        public string DirectLink
        {
            get { return _DirectLink; }
            private set { _DirectLink = value; }
        }

        public Mp3Video(string videoName, string singerName,string imgLink, string localLink)
        {
            _VideoName = videoName;
            _SingerName = singerName;
            _ImgLink = imgLink;
            _LocalLink = localLink;
            _DirectLink = "";
        }
        public Mp3Video(string localLink, string imgLink)
        {
            _VideoName = "";
            _SingerName = "";
            _DirectLink = "";
            _ImgLink = imgLink;
            _LocalLink = localLink;
        }

        public async Task SetAllInformation()
        {
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_LocalLink));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34)
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }
                if (_VideoName == "")
                    SetVideoName(source);
                if (_SingerName == "")
                    SetSingerName(source);
                if (_DirectLink == "")
                    SetDirectLink(source);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void SetVideoName(string source)
        {
            string pattern = @"(?<=<h1>).*?(?=</h1>)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            _VideoName = mc[0].Value;
        }

        private void SetSingerName(string source)
        {
            string pattern = @"(?<=<h2>).*?(?=</h2>)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            _VideoName = mc[0].Value;
        }

        private void SetDirectLink(string source)
        {
            string pattern = @"(?<=source src=).*?(?=type)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            string tempLink = mc[0].Value;

            int i = 0;
            while (i < tempLink.Length)
            {
                if (tempLink[i] == 32)
                {
                    tempLink = tempLink.Remove(i, 1);
                    i--;
                }
                i++;
            }

            _DirectLink = tempLink;
        }

    }
}
